package com.qst.service.impl;

import com.qst.ExamException;
import com.qst.dao.DAOFactory;
import com.qst.dao.DimensionDao;
import com.qst.dao.QuestionDao;
import com.qst.entity.PersonalityDimension;
import com.qst.service.DimensionService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DimensionServiceImpl implements DimensionService {

    private DimensionDao dimensionDAO = DAOFactory.getDao(DimensionDao.class);

    private QuestionDao questionDAO = DAOFactory.getDao(QuestionDao.class);

    @Override
    public List<PersonalityDimension> findDimensionByAssessment(int assessmentId) {
        return dimensionDAO.findByAssessment(assessmentId);
    }

    @Override
    public PersonalityDimension findDimensionById(int id) {
        return dimensionDAO.findById(id);
    }

    @Override
    public void saveDimension(PersonalityDimension kp) {
        PersonalityDimension temp = dimensionDAO.findByAssessmentAndTitle(kp.getAssessmentId(), kp.getTitle());
        if (temp == null) {
            dimensionDAO.insert(kp);

        } else {
            throw new ExamException("性格维度名称已存在");
        }
    }

    @Override
    public void deleteDimension(int id) {
        int count = questionDAO.findCountByDimension(id);
        if (count > 0) {
            throw new ExamException("本知识点已录入考题，不能再删除");
        }
        dimensionDAO.delete(id);
    }

    @Override
    public void updateDimension(PersonalityDimension kp) {
        PersonalityDimension temp = dimensionDAO.findByAssessmentAndTitle(kp.getAssessmentId(), kp.getTitle());
        if (temp == null || temp.getId() == kp.getId()) {
            dimensionDAO.update(kp);
        } else {
            throw new ExamException("本性格维度名称已存在");
        }
    }
}
